package com.jincp.filepicker.widgets;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.jincp.common.brvah.BaseQuickAdapter;
import com.jincp.common.brvah.layoutmanager.MyLinearLayoutManager;
import com.jincp.common.dialog.BaseDialogBuilder;
import com.jincp.common.dialog.BaseDialogFragment;
import com.jincp.filepicker.Constant;
import com.jincp.filepicker.R;
import com.jincp.filepicker.adapter.DirectoryAdapter;

import java.util.List;

/**
 * @ProjectName: filepicker.
 * @ClassName: PickDirectoryDialog.
 * @Description: .
 * @Author: jincp.
 * @CreateDate: 2019/3/23 10:15.
 */

public class PickDirectoryDialog extends BaseDialogFragment {

    private static List directoryList;

    public static PickDirectoryDialog newInstance(Builder builder) {
        PickDirectoryDialog dialog = new PickDirectoryDialog();
        directoryList = builder.directoryList;
        Bundle bundle = getArgumentBundle(builder);
        bundle.putInt("DirectoryType", builder.directoryType);
        dialog.setArguments(bundle);
        return dialog;
    }

    @Override
    protected void convertView(@Nullable View container, BaseDialogFragment dialogFragment) {
        int directory =  getArguments().getInt("DirectoryType");
        RecyclerView recyclerView = container.findViewById(R.id.recyclerView);
        LinearLayoutManager linearLayoutManager = new MyLinearLayoutManager(getActivity());
        recyclerView.setHasFixedSize(true);
        recyclerView.setLayoutManager(linearLayoutManager);
        DirectoryAdapter adapter = new DirectoryAdapter(directoryList, directory);
        adapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                ((DirectoryAdapter) adapter).setSingleChoice(position);
                if (mDialogResultListener != null) {
                    mDialogResultListener.result(PickDirectoryDialog.this, position);
                }
                dismissAllowingStateLoss();
            }
        });
        recyclerView.setAdapter(adapter);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.dialog_pick_directory;
    }

    public static class Builder extends BaseDialogBuilder<Builder, PickDirectoryDialog> {

        private List directoryList;

        private int directoryType;

        public Builder setDirectoryList(List directoryList) {
            this.directoryList = directoryList;
            return this;
        }

        public Builder setDirectoryType(int directoryType) {
            this.directoryType = directoryType;
            return this;
        }

        @Override
        public PickDirectoryDialog build() {
            return PickDirectoryDialog.newInstance(this);
        }
    }
}
